package com.huanqiu.mylib.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huanqiu.mylib.R;

/**
 * https://www.jianshu.com/p/705a6cb6bfee
 */
public class CommonHeaderView extends RelativeLayout {

    private String type = "common";

    private TextView text_center;
    private TextView text_left;
    private TextView text_right;
    private ImageView img_left;
    private ImageView img_right;

    public CommonHeaderView(Context context) {
        super(context);
        initView(context);
        showView();
    }

    public CommonHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initAttrs(context, attrs);
        showView();
    }

    public CommonHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CommonHeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_common_header, this, true);
        text_center = (TextView) findViewById(R.id.text_center);
        text_left = (TextView) findViewById(R.id.text_left);
        text_right = (TextView) findViewById(R.id.text_right);
        img_left = (ImageView) findViewById(R.id.img_left);
        img_right = (ImageView) findViewById(R.id.img_right);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonHeaderView);
        // 获取 left_text / right_text 属性
        String type = mTypedArray.getString(R.styleable.CommonHeaderView_type);
        String title = mTypedArray.getString(R.styleable.CommonHeaderView_title);

        if ("back".equals(type)) {
            img_left.setVisibility(VISIBLE);
            img_left.setImageResource(R.drawable.icon_back);
        } else {
            String left_text = mTypedArray.getString(R.styleable.CommonHeaderView_left_text);
            String right_text = mTypedArray.getString(R.styleable.CommonHeaderView_right_text);
            int left_img = mTypedArray.getResourceId(R.styleable.CommonHeaderView_left_img, 0);
            int right_img = mTypedArray.getResourceId(R.styleable.CommonHeaderView_right_img, 0);

            if (left_text != null) {
                text_left.setVisibility(VISIBLE);
                text_left.setText(left_text);
            } else {
                if (left_img != 0) {
                    img_left.setVisibility(VISIBLE);
                    img_left.setImageResource(left_img);
                }
            }

            if (right_text != null) {
                text_right.setVisibility(VISIBLE);
                text_right.setText(right_text);
            } else {
                if (right_img != 0) {
                    img_right.setVisibility(VISIBLE);
                    img_right.setImageResource(right_img);
                }
            }
        }

        mTypedArray.recycle();

        text_center.setText(title);
    }

    private void showView() {

    }

    public void setTitle(String text) {
        text_center.setText(text);
    }

    public void setLeftListener(OnClickListener onClickListener) {
        text_left.setOnClickListener(onClickListener);
        img_left.setOnClickListener(onClickListener);
    }

    public void setRightListener(OnClickListener onClickListener) {
        text_right.setOnClickListener(onClickListener);
        img_right.setOnClickListener(onClickListener);
    }

}
